using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class changeRoom : MonoBehaviour
{
    public Vector3 cameraChangePos; // Сдвиг позиции камеры
    public Vector3 playerChangePos; // Сдвиг позиции игрока
    private Camera cam; // Объект камера
    private GameObject SecondPlayer;
    private GameObject Player;

    private void Start() {
        // Получение компонента одноимённого камеры
        cam = Camera.main.GetComponent<Camera>();
        if (PlayerPrefs.GetInt("playersCount") > 1) SecondPlayer = GameObject.FindGameObjectWithTag("SecondPlayer");
        Player = GameObject.FindGameObjectWithTag("Player");
    }

    private void OnTriggerEnter2D(Collider2D other) {
        // При соприкосновении триггера с игроком камера и игрок изменяют своё местоположение в пространстве
        if (other.CompareTag("Player")) {
            other.transform.position += playerChangePos;
            cam.transform.position += cameraChangePos;
            if (PlayerPrefs.GetInt("playersCount") > 1) SecondPlayer.transform.position = other.transform.position;
        }
        if (other.CompareTag("SecondPlayer")) {
            other.transform.position += playerChangePos;
            cam.transform.position += cameraChangePos;
            Player.transform.position = other.transform.position;
        }
    }
}
